﻿using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;

namespace TestSpyApi
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (txtTargetHandle.TextLength < 1)
            {
                MessageBox.Show("Please input Control`s Handle code!");
                return;
            }
            IntPtr Hwnd = new IntPtr(Convert.ToInt32(txtTargetHandle.Text, 16));
            label1.Text = new GetTextClass().GetControlText(Hwnd);
        }

        private void btnScreenshot_Click(object sender, EventArgs e)
        {
            if (txtTargetHandle.TextLength < 1)
            {
                MessageBox.Show("Please input Control`s Handle code!");
                return;
            }
            IntPtr Hwnd = new IntPtr(Convert.ToInt32(txtTargetHandle.Text, 16));
            var rect = new Screenshot.Rect();
            Screenshot.GetWindowRect(Hwnd, ref rect);

            int width = rect.right - rect.left;
            int height = rect.bottom - rect.top;

            var bmp = new Bitmap(width, height, PixelFormat.Format32bppArgb);
            Graphics graphics = Graphics.FromImage(bmp);
            graphics.CopyFromScreen(rect.left, rect.top, 0, 0, new Size(width, height), CopyPixelOperation.SourceCopy);

            bmp.Save("d:\\test.png", ImageFormat.Png);
            label1.Text = string.Format("Image file is saved in {0}", "c:\\test.png");
        }

        private void btnPrintControl_Click(object sender, EventArgs e)
        {
            if (txtTargetHandle.TextLength < 1)
            {
                MessageBox.Show("Please input Control`s Handle code!");
                return;
            }
            IntPtr Hwnd = new IntPtr(Convert.ToInt32(txtTargetHandle.Text, 16));
            var bmp = PrintControl.PrintWindow(Hwnd);
            bmp.Save("d:\\test.png", ImageFormat.Png);
            label1.Text = string.Format("Image file is saved in {0}", "c:\\test.png");
        }
    }
}
